<?php
/**
 * This is the actual Language Tandem System.
 * 
 * What you need to know to understand this script:
 * 
 * 		Actions: see the ActionFramework.
 * 
 * 		$_SESSION['user']: is set to null if not defined yet; otherwise it is a User instance.
 * 						   This can be either a LT participant or an admin.
 * 
 * 
 */

set_time_limit(30);

require_once 'version_fixes.php';
require_once 'installation_settings.php';


/* 
 * START DATABASE
 */
require_once 'db.php';
global $DB;
$DB = new LanguageTandemDatabase(DB_DATABASE, DB_USER, DB_PASSWORD, DB_HOST, DB_TABLE_PREFIX);
//$DB = new DummyLanguageTandemDatabase();

// Do some cleaning now and then
//if (rand(0, 1000) < 10) {
//	$DB->clean();
//}

/*
 * BUILD DISPATCHER
 */

require_once 'actions/start.cls.php';
require_once 'actions/login.cls.php';
require_once 'actions/login_by_url.cls.php';
require_once 'actions/signup.cls.php';
require_once 'actions/continue_signup.cls.php';
require_once 'actions/finish_signup.cls.php';
require_once 'actions/logout.cls.php';
require_once 'actions/signup.cls.php';
require_once 'actions/signoff_ask_confirm.cls.php';
require_once 'actions/signoff_confirmed.cls.php';
require_once 'actions/view_matching.cls.php';
require_once 'actions/edit_criteria.cls.php';
require_once 'actions/save_criteria.cls.php';
require_once 'actions/edit_profile.cls.php';
require_once 'actions/save_profile.cls.php';
require_once 'actions/edit_password.cls.php';
require_once 'actions/save_password.cls.php';
require_once 'actions/manage_rejection.cls.php';
require_once 'actions/send_message.cls.php';
require_once 'actions/reject_by_url.cls.php';
require_once 'actions/request_password_reset.cls.php';
require_once 'actions/try_reset_password.cls.php';
require_once 'actions/reset_password.cls.php';
require_once 'actions/static_page.cls.php';

require_once 'actions/admin.cls.php';
require_once 'actions/manage_block.cls.php';
require_once 'actions/remove_user.cls.php';

require_once 'ActionFramework/dispatcher.cls.php';
global $DISPATCHER;
$DISPATCHER = new ActionDispatcher();

$DISPATCHER->register_default(new StartAction());
$DISPATCHER->register_login(new StartAction('To obtain the rights for this action, you need to log in with an appropriate account.'));
$DISPATCHER->register_blocked(new StartAction('Your account is no longer accessible because it has been deactivated.'));
$DISPATCHER->register(new LoginAction(), AuthLevel::NONE);
$DISPATCHER->register(new LoginByUrlAction(), AuthLevel::NONE);
$DISPATCHER->register(new SignupAction(), AuthLevel::NONE);
$DISPATCHER->register(new ContinueSignupAction(), AuthLevel::NONE);
$DISPATCHER->register(new FinishSignupAction(), AuthLevel::NONE);
$DISPATCHER->register(new LogoutAction(), AuthLevel::NONE); // supresses unnecessary errors, e.g. if logged out user logs out again.
$DISPATCHER->register(new SendMessageAction(), AuthLevel::NONE);
$DISPATCHER->register(new RejectByUrlAction(), AuthLevel::NONE);
$DISPATCHER->register(new RequestPasswordResetAction(), AuthLevel::NONE);
$DISPATCHER->register(new TryResetPasswordAction(), AuthLevel::NONE);
$DISPATCHER->register(new ResetPasswordAction(), AuthLevel::NONE);
$DISPATCHER->register(new StaticPageAction(), AuthLevel::NONE);

$DISPATCHER->register(new SignoffAskConfirmAction(), AuthLevel::USER);
$DISPATCHER->register(new SignoffConfirmedAction(), AuthLevel::USER);
$DISPATCHER->register(new ViewMatchingAction(), AuthLevel::USER);
$DISPATCHER->register(new EditCriteriaAction(), AuthLevel::USER);
$DISPATCHER->register(new SaveCriteriaAction(), AuthLevel::USER);
$DISPATCHER->register(new EditProfileAction(), AuthLevel::USER);
$DISPATCHER->register(new SaveProfileAction(), AuthLevel::USER);
$DISPATCHER->register(new EditPasswordAction(), AuthLevel::USER);
$DISPATCHER->register(new SavePasswordAction(), AuthLevel::USER);
$DISPATCHER->register(new ManageRejectionAction(), AuthLevel::USER);

$DISPATCHER->register(new AdminAction(), AuthLevel::ADMIN);
$DISPATCHER->register(new ManageBlockAction(), AuthLevel::ADMIN);
$DISPATCHER->register(new RemoveUserAction(), AuthLevel::ADMIN);


/*
 * SESSION
 */
session_start();
if (isset($_SESSION['user'])) {
	$DB->update_user($_SESSION['user']);
	if ($_SESSION['user']->admin) {
		$auth_level = AuthLevel::ADMIN;
	}
	else if ($_SESSION['user']->blocked) {
		// User had been banned, end session
		$_SESSION['user'] = null;
		$DISPATCHER->dispatch_blocked();
	}
	else {
		$auth_level = AuthLevel::USER;
	}
}
else {
	$_SESSION['user'] = null;
	$auth_level = AuthLevel::NONE;
}


/*
 * HANDLE REQUEST
 */

$action_requested = isset($_REQUEST['action']) ? $_REQUEST['action']: null;

$DISPATCHER->dispatch($action_requested, $auth_level);

?>